//
//  LGRecommendDoctorController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/3/24.
//  聊天咨询 -- 推荐医生

#import "LGRecommendDoctorController.h"
//医生个人信息
#import "LGPersionalDataController.h"
#import "LGRecomendDoctorCell.h"

@interface LGRecommendDoctorController ()<UITableViewDataSource,UITableViewDelegate>
/**   */
@property (nonatomic , strong)UITextField *searchTF;
/**   */
@property (nonatomic , strong)UIView *leftView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *sendButton;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , assign)NSInteger index;
@end

@implementation LGRecommendDoctorController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.index = -1;
    self.title = @"推荐医生";
    [self.view addSubview:self.searchTF];
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.sendButton];
    [self emptyViewWithSuperView:self.view frame:CGRectMake(0, kMinY(self.tableView.frame), Screen_W, Screen_H-kMinY(self.tableView.frame))];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/im/doctorList" parameters:@{@"keyword":self.searchTF.text} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            self.dataArray = returnData;
            [self.tableView reloadData];
        }
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView3" content:@"暂无推荐医生" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView3" content:@"暂无推荐医生" offset:0];
        }
    }];
}


-(void)selectBtnAction:(UIButton *)sender{
    self.index = sender.tag-1000;
    [self.tableView reloadData];
}

-(void)sendButtonAction{
    if(self.index>=0){
        NSDictionary *tempDic = self.dataArray[self.index];
        [RequestUtil withPOST:@"/doctor/im/sendDoctorRecommendMessage" parameters:@{@"appointment_id":self.appointment_id,@"timID":self.timID,@"doctor_ids":LGNSString(tempDic[@"id"])} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if(code == 200){
                [self.navigationController popViewControllerAnimated:YES];
            }
        } error:^(NSError *error) {
            
        }];
    }else{
        [TooltipView showMessage:@"请选择要推荐的医生" offset:0];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGRecomendDoctorCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    cell.selectBtn.tag = indexPath.row+1000;
    cell.selectBtn.selected = (self.index == indexPath.row)?YES:NO;
    [cell.selectBtn addTarget:self action:@selector(selectBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    LGPersionalDataController *controller = [[LGPersionalDataController alloc]init];
    controller.userId = LGNSString(tempDic[@"id"]);
    [self.navigationController pushViewController:controller animated:YES];
}


-(void)textFieldEditingChanged:(UITextField *)textField{
    [self requestData];
}


-(UITextField *)searchTF{
    if(!_searchTF){
        _searchTF = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(53))];
        [_searchTF setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _searchTF.backgroundColor = [UIColor whiteColor];
        _searchTF.tintColor = [UIColor colorWithString:@"#333333"];
        _searchTF.textColor = [UIColor colorWithString:@"#333333"];
        _searchTF.textAlignment = NSTextAlignmentLeft;
        _searchTF.keyboardType = UIKeyboardTypeDefault;
        _searchTF.placeholder = @"请输入医生姓名搜索";
        _searchTF.font = LGFont(15);
        _searchTF.leftViewMode = UITextFieldViewModeAlways;
        _searchTF.leftView = self.leftView;
        [_searchTF addTarget:self action:@selector(textFieldEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    }
    return _searchTF;
}

-(UIView *)leftView{
    if(!_leftView){
        _leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, viewPix(36), viewPix(53))];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(12), viewPix(19), viewPix(16), viewPix(16))];
        imageView.image = [UIImage imageNamed:@"searchGray16"];
        [_leftView addSubview:imageView];
    }
    return _leftView;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, viewPix(80), Screen_W, Screen_H-topBarHeight-viewPix(80)-viewPix(86)) style:UITableViewStylePlain];
        [_tableView registerClass:[LGRecomendDoctorCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = viewPix(153);
        _tableView.rowHeight = viewPix(153);
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

-(UIButton *)sendButton{
    if(!_sendButton){
        _sendButton = [UIButton buttonWithTitle:@"发送给患者" topMargin:kMaxY(self.tableView.frame)+viewPix(5)];
        [_sendButton addTarget:self action:@selector(sendButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sendButton;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
